﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
// Experimental/Exploratory
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using System.Threading;
using System.IO;

namespace BrainTechLLC
{
    [DataContract]
    public enum MediaType
    {
        [EnumMember]
        None = 0,
        [EnumMember]
        Image = 1,
        [EnumMember]
        Sound = 2,
    };

    public static class MediaLibrary
    {

    }

    [Serializable]
    [DataContract]
    public class Media : BaseClass
    {
        [NonSerialized]
        private object _lockObject;

        [NonSerialized]
        public string _basePath;

        public string BasePath
        {
            get { return _basePath; }
            set
            {
                if (!_basePath.AreEqual(value))
                {
                    OnPropertyChanging("BasePath", _basePath, value);
                    _basePath = value;
                    OnPropertyChanged("BasePath", _basePath, value);
                }
            }
        }

        [DataMember]
        public string _fileName;

        public string FileName
        {
            get { return _fileName; }
            set
            {
                if (!_fileName.AreEqual(value))
                {
                    OnPropertyChanging("FileName", _fileName, value);
                    _fileName = value;
                    OnPropertyChanged("FileName", _fileName, value);
                }
            }
        }

        [DataMember]
        public string _url;

        public string Url
        {
            get { return _url; }
            set
            {
                if (!_url.AreEqual(value))
                {
                    OnPropertyChanging("Url", _url, value);
                    _url = value;
                    OnPropertyChanged("Url", _url, value);
                }
            }
        }

        [DataMember]
        public MediaType _mediaType;

        public MediaType MediaType
        {
            get { return _mediaType; }
            set
            {
                if (!_mediaType.AreEqual(value))
                {
                    OnPropertyChanging("MediaType", _mediaType, value);
                    _mediaType = value;
                    OnPropertyChanged("MediaType", _mediaType, value);
                }                
            }
        }

        public int ContentLength
        {
            get
            {
                return (_bytes == null) ? 0 : _bytes.Length;
            }
        }

        [DataMember]
        public byte[] _bytes;

        public byte[] Bytes
        {
            get
            {
#if NO_SILVERLIGHT
                if (_bytes == null)
                {
                    if (_lockObject == null)
                        Interlocked.CompareExchange<object>(ref _lockObject, new object(), null);

                    lock (_lockObject)
                    {
                        if (_bytes == null)
                        {
                            string fileName = (string.IsNullOrEmpty(_basePath) ? string.Empty : _basePath) + _fileName;

                            try
                            {
                                if (File.Exists(fileName))
                                {
                                    byte[] fileBytes = File.ReadAllBytes(fileName);
                                    Interlocked.Exchange<byte[]>(ref _bytes, fileBytes);
                                }
                            }
                            catch (Exception ex)
                            {
                                ex.Log("Could not find media " + fileName, LogSeverity.Warning, true);
                            }
                        }
                    }
                }
#endif
                return _bytes;
            }
            set
            {
                if (!_bytes.AreEqual(value))
                {
                    OnPropertyChanging("Bytes", _bytes, value);
                    _bytes = value;
                    OnPropertyChanged("Bytes", _bytes, value);
                }                                
            }
        }
    }
}
